package spring.cloud.tasks.executor.node;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter
@Getter
public class Node {

    public static final String JOBS_NODE = "$tasks";
    public static final String TASKS_NODE_PATH = "/" + JOBS_NODE;
    private static final String SATURN_EXECUTORS_NODE = "$SaturnExecutors";
    public static final String CONFIG_VERSION_PATH = "/" + SATURN_EXECUTORS_NODE + "/config/version";
    public static final String SATURNEXECUTORS_PATH = "/" + SATURN_EXECUTORS_NODE;
    private static final String EXECUTORS = "executors";
    public static final String EXECUTORSNODE_PATH = "/" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS;
    private static final String HOST = "host";
    private static final String LEADER = "leader";
    public static final String LEADER_HOSTNODE_PATH = "/" + SATURN_EXECUTORS_NODE + "/" + LEADER + "/" + HOST;
    public static final String LEADERNODE_PATH = "/" + SATURN_EXECUTORS_NODE + "/" + LEADER;
    private static final String LATCH = "latch";
    public static final String LEADER_LATCHNODE_PATH = "/" + SATURN_EXECUTORS_NODE + "/" + LEADER + "/" + LATCH;
    private static final String SHARDING = "sharding";
    public static final String SHARDING_COUNT_PATH = "/" + SATURN_EXECUTORS_NODE + "/" + SHARDING + "/" + "count";
    public static final String SHARDINGNODE_PATH = "/" + SATURN_EXECUTORS_NODE + "/" + SHARDING;
    private static final String CONTENT = "content";
    public static final String SHARDING_CONTENTNODE_PATH = "/" + SATURN_EXECUTORS_NODE + "/" + SHARDING + "/" + CONTENT;
    private static final String IP = "ip";
    public static final String EXECUTOR_IPNODE_PATH_REGEX =
            "/\\" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS + "/" + "[^/]*"
                    + "/" + IP;
    private static final String LOST_TRAFFIC = "lostTraffic";
    public static final String EXECUTOR_LOST_TRAFFIC_NODE_PATH_REGEX =
            "/\\" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS + "/" + "[^/]*"
                    + "/" + LOST_TRAFFIC;
    private static final String CLEAN = "clean";

    private static final String DCOS_NODE = "$DCOS";
    private static final String TASKS = "tasks";


    /**
     * 获取$SaturnExecutors/executors结点完整路径
     */
    public static String getExecutorsNodePath() {
        return "/" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS;
    }

    /**
     * 获取ip结点名称
     */
    public static String getIpNodeName() {
        return IP;
    }

    /**
     * 获取noTraffic结点名称
     */
    public static String getNoTrafficNodeName() {
        return LOST_TRAFFIC;
    }

    /**
     * 获取$SaturnExecutors/executors/xx结点完整路径
     */
    public static String getExecutorNodePath(String executor) {
        return "/" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS + "/" + executor;
    }

    /**
     * 获取$SaturnExecutors/executors/xx/ip结点完整路径
     */
    public static String getExecutorIpNodePath(String executor) {
        return "/" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS + "/" + executor + "/" + IP;
    }

    /**
     * 获取$SaturnExecutors/executors/xx/noTraffic结点完整路径
     *
     * @return true 已经被摘流量；false，otherwise；
     */
    public static String getExecutorLostTrafficNodePath(String executor) {
        return "/" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS + "/" + executor + "/" + LOST_TRAFFIC;
    }

    /**
     * 获取$SaturnExecutors/executors/xx/clean结点完整路径
     */
    public static String getExecutorCleanNodePath(String executor) {
        return "/" + SATURN_EXECUTORS_NODE + "/" + EXECUTORS + "/" + executor + "/" + CLEAN;
    }


    /**
     * 从路径中抽取出executorName
     */
    public static String getExecutorNameByIpPath(String path) {
        return getExecutorNameByPath(path, getIpNodeName());
    }

    /**
     * 从路径中抽取出executorName
     */
    public static String getExecutorNameByNoTrafficPath(String path) {
        return getExecutorNameByPath(path, getNoTrafficNodeName());
    }

    private static String getExecutorNameByPath(String path, String nodeName) {
        int lastIndexOf = path.lastIndexOf("/" + nodeName);
        String substring = path.substring(0, lastIndexOf);
        int lastIndexOf2 = substring.lastIndexOf('/');
        return substring.substring(lastIndexOf2 + 1);
    }

    /**
     * 获取$SaturnExecutors/sharding结点完整路径
     */
    public static String getExecutorShardingNodePath(String nodeName) {
        return "/" + SATURN_EXECUTORS_NODE + "/" + SHARDING + "/" + nodeName;
    }

    /**
     * 获取$SaturnExecutors/sharding/content结点完整路径
     */
    public static String getShardingContentElementNodePath(String element) {
        return "/" + SATURN_EXECUTORS_NODE + "/" + SHARDING + "/" + CONTENT + "/" + element;
    }

    /**
     * 获取作业结点完整路径
     */
    public static String getJobNodePath(String jobName) {
        return String.format("/%s/%s", JOBS_NODE, jobName);
    }

    /**
     * 获取$Jobs/xx/config/shardingTotalCount结点完整路径
     */
    public static String getTaskConfigShardingTotalCountNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s", JOBS_NODE, jobName, "config", "shardingTotalCount");
    }

    /**
     * 获取$Jobs/xx/config/loadLevel结点完整路径
     */
    public static String getTaskConfigLoadLevelNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s", JOBS_NODE, jobName, "config", "loadLevel");
    }

    /**
     * 获取$Jobs/xx/config/preferList结点完整路径
     */
    public static String getTaskConfigPreferListNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s", JOBS_NODE, jobName, "config", "preferList");
    }

    /**
     * 获取$Jobs/xx/config/enabled结点完整路径
     */
    public static String getJobConfigEnableNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s", JOBS_NODE, jobName, "config", "enabled");
    }


    /**
     * 获取$Jobs/xx/config/useDispreferList结点完整路径
     */
    public static String getTaskConfigUseDispreferListNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s", JOBS_NODE, jobName, "config", "useDispreferList");
    }

    /**
     * 获取$Jobs/xx/config/forceShard结点完整路径
     */
    public static String getTaskConfigForceShardNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s", JOBS_NODE, jobName, "config", "forceShard");
    }

    /**
     * 获取$Jobs/xx/leader/sharding/necessary完整路径
     */
    public static String getTaskLeaderShardingNecessaryNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s/%s", JOBS_NODE, jobName, "leader", "sharding", "necessary");
    }

    /**
     * 获取$Jobs/xx/leader/sharding完整路径
     */
    public static String getTaskLeaderShardingNodePath(String jobName) {
        return String.format("/%s/%s/%s/%s", JOBS_NODE, jobName, "leader", "sharding");
    }

    /**
     * 获取$Jobs/xx/execution完整路径
     */
    public static String getJobExecutionNodePath(final String jobName) {
        return String.format("/%s/%s/execution", JOBS_NODE, jobName);
    }

    /**
     * 获取$Jobs/xx/servers完整路径
     */
    public static String getTaskExecutorsNodePath(String jobName) {
        return String.format("/%s/%s/servers", JOBS_NODE, jobName);
    }

    /**
     * 获取$Jobs/xx/servers/yy完整路径
     */
    public static String getTaskExecutorNodePath(String jobName, String executorName) {
        return String.format("/%s/%s/servers/%s", JOBS_NODE, jobName, executorName);
    }

    /**
     * Get the $/Jobs/jobName/servers/executorName/status node path
     */
    public static String getJobServersExecutorStatusNodePath(String jobName, String executorName) {
        return String.format("/%s/%s/servers/%s/%s", JOBS_NODE, jobName, executorName, "status");
    }

    public static String getTaskExecutorStatusNodePathRegex(String jobName) {
        return "/\\" + JOBS_NODE + "/" + jobName + "/" + "servers" + "/" + "[^/]*" + "/" + "status";
    }

    public static String getJobServersExecutorNameByStatusPath(String path) {
        int beginIndexOf = path.lastIndexOf("/servers/") + 9;
        int lastIndexOf = path.lastIndexOf("/status");
        return path.substring(beginIndexOf, lastIndexOf);
    }

    /**
     * 获取$DCOS/tasks/xxx完整路径
     */
    public static String getDcosTaskNodePath(String task) {
        return String.format("/%s/%s/%s", DCOS_NODE, TASKS, task);
    }

}
